<?php
class User extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		$this->load->model ( 'user_model' );
		$this->load->model ( 'blog_model' );
		$this->load->library ( 'datelib' );
	}
	public function info($uid = 'self') {
		$cur_uid = $this->session->userdata ( 'user_id' );
		if ($uid == 'self') {
			$uid = $cur_uid;
		}
		
		$data = array (
				'main_url' => 'index.php/user/info_area?uid=' . $uid,
				'js_load' => array (
						'public/script/dashboard.js' 
				) 
		);
		if ($uid == $cur_uid) {
			$data ['loc'] = 'account';
		}
		
		$data ['user_info'] = $this->user_model->get_user ( $cur_uid );
		$this->load->view ( 'template/header', $data );
		
		$this->load->view ( 'framework/main', $data );
		$this->set_post ();
		$this->load->view ( 'template/footer' );
	}
	public function info_area() {
		$uid = $this->input->get ( 'uid' );
		$cur_uid = $this->session->userdata ( 'user_id' );
		if ($uid == 'self') {
			$uid = $cur_uid;
		}
		$data ['user'] = $this->user_model->get_user ( $uid );
		$blog_list ['own'] = $this->blog_model->get_own_blog_list ( $uid );
		$blog_list ['manage'] = $this->blog_model->get_manage_blog_list ( $uid );
		$data ['blogs'] = $blog_list;
		$this->load->view ( 'user/info', $data );
	}
	public function register($new = '0') {
		if ($new == 'new') {
			$uname = $this->input->post ( 'username' );
			$password = $this->input->post ( 'password' );
			$email = $this->input->post ( 'email' );
			$blogname = $this->input->post ( 'blogname' );
			$user = $this->user_model->get_id_by_email ( $email );
			if ($user) {
				echo - 1;
			} else {
				$user = $this->user_model->get_id_by_name ( $uname );
				if ($user) {
					echo - 1;
				} else {
					$uid = $this->user_model->add_user ( $uname, $email, $password );
					$bid = $this->blog_model->create_blog ( $blogname, $uid );
					$this->session->set_userdata ( 'user_id', $uid );
					$this->session->set_userdata ( 'blog_id', $bid );
					echo $uid;
				}
			}
		} else {
			$data = array (
					'title' => 'Sgin up' 
			);
			$this->load->view ( 'template/header', $data );
			$this->load->view ( 'user/register' );
			$this->load->view ( 'template/footer' );
		}
	}
	public function check_email($email) {
		$user = $this->user_model->get_id_by_email ( $email );
		if ($user) {
			echo 'invalid';
		} else {
			echo 'valid';
		}
	}
	public function check_name($name) {
		$user = $this->user_model->get_id_by_name ( $name );
		if ($user) {
			echo 'invalid';
		} else {
			echo 'valid';
		}
	}
	public function login() {
		if (! $this->input->post ( 'email' )) {
			$data = array (
					'title' => 'Sign in' 
			);
			$this->load->view ( 'template/header', $data );
			$this->load->view ( 'user/login' );
			$this->load->view ( 'template/footer' );
		} else {
			$email = $this->input->post ( 'email' );
			$password = $this->input->post ( 'password' );
			if ($this->user_model->validate_user ( $email, $password )) {
				$uid = $this->user_model->get_id_by_email ( $email );
				$this->session->set_userdata ( 'user_id', $uid );
				
				$blog_info = $this->blog_model->default_blog ( $uid );
				
				if ($blog_info) {
					$this->session->set_userdata ( 'blog_id', $blog_info ['bid'] );
				}
				echo 'pass';
			} else {
				echo 'deny';
			}
		}
	}
	public function logout() {
		$this->session->sess_destroy ();
		$this->load->helper ( 'url' );
		redirect ( 'blog', 'refresh' );
	}
	public function get() {
		$email = $this->input->get ( 'email' );
		$uname = $this->input->get ( 'name' );
		$uid = - 1;
		if ($email) {
			$uid = $this->user_model->get_id_by_email ( $email );
		} elseif ($uname) {
			$uid = $this->user_model->get_id_by_name ( $uname );
		}
		if ($uid > 0) {
			echo json_encode ( $this->user_model->get_user ( $uid ) );
		} else {
			echo 'fail';
		}
	}
	public function avatar($uid) {
	}
	private function set_post() {
		$bid = $this->session->userdata ( 'blog_id' );
		$binfo = $this->blog_model->get_blog ( $bid );
		$blogdata ['blog_name'] = $binfo ['bname'];
		$data ['blog_name'] = $binfo ['bname'];
		$this->load->view ( 'feed/post', $blogdata );
	}
}

?>